package org.dromara.learning_xq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.learning_xq.entity.AsTeachingMaterial;
import org.dromara.learning_xq.entity.dto.AsTeachingMaterialDto;
import org.dromara.learning_xq.mapper.AsTeachingMaterialMapper;
import org.dromara.learning_xq.service.AsTeachingMaterialService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AsTeachingMaterialServiceimpl extends ServiceImpl<AsTeachingMaterialMapper, AsTeachingMaterial> implements AsTeachingMaterialService {

    @Autowired
    private AsTeachingMaterialMapper asteachingMaterialMapper;

    @Override
    public int insertAsTeachingMaterial(AsTeachingMaterial asTeachingMaterial) {
        return asteachingMaterialMapper.insert(asTeachingMaterial);
    }

    @Override
    public int updateAsTeachingMaterial(AsTeachingMaterial asTeachingMaterial) {
        return asteachingMaterialMapper.updateById(asTeachingMaterial);
    }

    @Override
    public AsTeachingMaterial findById(Long id) {
        return asteachingMaterialMapper.selectById(id);
    }

    /**
     * 查询所有教学材料
     *
     * @param asTeachingMaterialDto 教学材料DTO对象，携带了查询条件
     * @return 返回教学材料DTO列表
     * <p>
     * 说明：本方法通过Lambda表达式构建查询条件，调用Mapper层的selverAll方法查询所有匹配的教学材料，
     * 并通过Stream流操作将查询结果转换为新的DTO列表返回。
     */
    @Override
    public List<AsTeachingMaterialDto> selverAll(AsTeachingMaterialDto asTeachingMaterialDto) {
        // 构建Lambda风格的查询条件封装对象
        LambdaQueryWrapper<AsTeachingMaterialDto> materialDtoLambdaQueryWrapper = new LambdaQueryWrapper<>();

        // 如果内容ID不为空，则添加等于查询条件
        if (asTeachingMaterialDto.getContentId() != null) {
            materialDtoLambdaQueryWrapper.eq(AsTeachingMaterialDto::getContentId, asTeachingMaterialDto.getContentId());
        }
        // 如果章节归属不为空，则添加等于查询条件
        if (!asTeachingMaterialDto.getChapterAffiliation().isEmpty()) {
            materialDtoLambdaQueryWrapper.eq(AsTeachingMaterialDto::getChapterAffiliation, asTeachingMaterialDto.getChapterAffiliation());
        }
        // 如果归属章节不为空，则添加等于查询条件
        if (!asTeachingMaterialDto.getBelongingSection().isEmpty()) {
            materialDtoLambdaQueryWrapper.eq(AsTeachingMaterialDto::getBelongingSection, asTeachingMaterialDto.getBelongingSection());
        }
        // 调用Mapper层的selverAll方法，根据查询条件获取教学材料列表
        List<AsTeachingMaterialDto> asTeachingMaterial = asteachingMaterialMapper.selverAll(asTeachingMaterialDto);

        // 将查询结果通过Stream流操作，复制每个教学材料对象的属性到新的DTO对象，并收集为列表返回
        return asTeachingMaterial.stream().map(teachingMaterial -> {
            AsTeachingMaterialDto asTeachingMaterials = new AsTeachingMaterialDto();
            BeanUtils.copyProperties(teachingMaterial, asTeachingMaterials);
            return asTeachingMaterials;
        }).collect(Collectors.toList());
    }


    /**
     * 根据标题名称模糊查询教学资料
     * 此方法通过模糊匹配标题名称，查询数据库中的教学资料，并将结果转换为DTO列表返回
     *
     * @param titleName 教学资料的标题名称，用于模糊查询
     * @return 返回一个包含教学资料DTO的列表
     */
    @Override
    public List<AsTeachingMaterialDto> selverFuzzyQuery(String titleName) {
        // 创建 LambdaQueryWrapper 对象，用于构建查询条件
        LambdaQueryWrapper<AsTeachingMaterial> queryWrapper = new LambdaQueryWrapper<>();

        // 当标题名称不为空时，添加模糊查询条件
        if (StringUtils.isNotEmpty(titleName)) {
            queryWrapper.like(AsTeachingMaterial::getTitleName, titleName);
        }

        // 执行查询，获取符合条件的教学资料列表
        List<AsTeachingMaterial> asTeachingMaterialList = asteachingMaterialMapper.selectList(queryWrapper);

        // 将查询到的实体列表转换为 DTO 列表并返回
        return asTeachingMaterialList.stream().map(teachingMaterial -> {
            AsTeachingMaterialDto asTeachingMaterials = new AsTeachingMaterialDto();
            // 将实体对象的属性复制到 DTO 对象
            BeanUtils.copyProperties(teachingMaterial, asTeachingMaterials);
            return asTeachingMaterials;
        }).collect(Collectors.toList());
    }

    @Override
    public AsTeachingMaterialDto selverId(Long id) {
        return asteachingMaterialMapper.selverId(id);
    }

    public Page<AsTeachingMaterial> selverPaginationQuery(int pageNum, int pageSize) {
        Page<AsTeachingMaterial> page = new Page<>(pageNum, pageSize);
        return asteachingMaterialMapper.selectPage(page, null); // 这里的 null 是查询条件
    }

    @Override
    public boolean removeId(Long id) {
        try {
            // 执行删除操作，返回受影响的记录数
            int rowsAffected = asteachingMaterialMapper.deleteById(id);
            // 判断是否删除成功（受影响的记录数大于 0）
            return rowsAffected > 0;
        } catch (Exception e) {
            // 记录日志或处理异常
            // logger.error("Error deleting record with id: " + id, e);
            return false;
        }
    }

}
